package com.china08.yxyapi.api.manage.tec.teaching.lesson.school;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.api.manage.tec.teaching.lesson.school.YxyLessonsSchoolCrtChaperController.RespModel;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonItemRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class YxyLessonsSchoolUpdItemOrderNumController extends YxyApiBaseController {

    @Autowired
    YxyLessonsSchoolLessonItemRepository itemRepository;

    @ApiOperation("")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/school/item/orderNum/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        List<Chapter> list = reqModel.list;
        for (Chapter chapter : list) {
            String chapterId = chapter.chapterId;
            List<Item> itemList = chapter.itemList;
            for (Item item : itemList) {
                String itemId = item.itemId;
                Integer orderNum = item.orderNum;
                YxyLessonsSchoolLessonItem it = itemRepository.findById(itemId);
                if (null != it) {
                    it.setChapterId(chapterId);
                    it.setOrderNum(orderNum);
                    itemRepository.save(it);
                }
            }
        }
        return null;
    }

    public static class Item {
        public String itemId;
        public Integer orderNum;
    }

    public static class Chapter {
        public String chapterId;
        public List<Item> itemList;
    }

    public static class ReqModel extends YxyApiReqModel {
        public List<Chapter> list;
    }
}
